package com.motu.monstercity.server.game.logic;

import com.motu.monstercity.protocol.Common.PBUserData;
import com.motu.monstercity.protocol.CsGameSystem;
import com.motu.monstercity.protocol.CsGameSystem.CSGameSystemRedPointResponse;
import com.motu.monstercity.server.game.base.*;
import com.motu.monstercity.server.game.commondata.act.ActTopRanking;
import com.motu.monstercity.server.game.commondata.bag.*;
import com.motu.monstercity.server.game.commondata.maintask.MainTaskConstant;
import com.motu.monstercity.server.game.commondata.union.UnionConstant;
import com.motu.monstercity.server.game.log.LogType;
import com.motu.monstercity.server.game.log.MotuLogManager;
import com.motu.monstercity.server.game.log.ThinkingDataManager;
import com.motu.monstercity.server.game.userdata.*;
import com.motu.vertx.module.utility.exception.LockException;
import com.motu.vertx.module.utility.main.MainService;
import com.motu.vertx.module.utility.toolset.Tool;
import com.sun.xml.bind.v2.schemagen.xmlschema.Union;
import io.vertx.core.shareddata.Lock;

import java.util.ArrayList;
import java.util.List;

public class BagManager extends BagUserData {
    // 初始化 下发背包模块的用户数据
    public static void loadInitBagUserData(PBUserData.Builder pbUserData, UserInfo userInfo) {
        long userId = userInfo.getId();
        for (UserItem userItem : getUserItemList(userId)) { // 道具
            ProtoDataUtils.updatePBUserData(pbUserData, userItem, true);
        }

        for (UserChip userChip : getUserChipList(userId)) { // 碎片
            ProtoDataUtils.updatePBUserData(pbUserData, userChip, true);
        }
    }


    //  获取背包中的道具数量
    public static long getUserItemNum(UserInfo userInfo, int itemId, Object... param) {
        long itemNum = 0L;
        long userId = userInfo.getId();

        int itemType = GameConstant.getItemType(itemId);// itemid的类型
        if (itemType == GameConstant.REWARD_TYPE_CHIP_MAIN) {// 碎片
            UserChip userChip = getUserChip(userId, itemId);
            if (userChip == null) {
                return 0L;
            } else {
                return userChip.getNum();
            }
        }

        UserPower userPower = null;
        UserAffair userAffair = null;
        switch (itemId) {
            case ItemId.DIAMOND:// 钻石
                itemNum = userInfo.getDiamond();
                break;
            case ItemId.GOLD:// 金币
                userPower = GameUser.getUserPower(userId);
                userAffair = GameUser.getUserAffair(userId);
                BuildManager.countTotalEarnNow(null, userInfo, userPower, userAffair, false);// 按当前时间的临时收益结算一下城市收益
                itemNum = userInfo.getGold();
                break;
            case ItemId.USER_REPU:// 声望
                itemNum = userInfo.getRepu();
                break;
            case ItemId.PARTNER_EXP:// 干员经验
                userAffair = GameUser.getUserAffair(userId);
                itemNum = userAffair.getPartnerExp();
                break;
            case ItemId.DATING_EXP:// 干员经验
                if (param.length == 0) {
                    break;
                }
                UserSecretary userSecretary = getUserSecretary(userId, param);
                if (userSecretary != null) {
                    itemNum = userSecretary.getFetters();
                }
                break;
            case ItemId.FIGHT_SNATCH_MONEY:// 物资争夺的贸易币
                UserSnatchInfo userSnatchInfo = FightManager.getUserSnatchInfo(userId);
                itemNum = userSnatchInfo.getMoney();
                break;
            case ItemId.PERSON:// 人口
                UserCityInfo userCityInfo = BuildManager.getUserCityInfo(userId);
                itemNum = userCityInfo.getPeasonNum();
                break;
            case ItemId.ENERGY:
                itemNum = userInfo.getEnergy();
                break;
            case ItemId.TYPE_UNION_CONTRIBUTION:
                UserUnion userUnion = UnionManager.getUserUnion(userId);
                if (userUnion.getUnionId() != 0) {
                    UnionInfo unionInfo = UnionManager.getUnionInfo(userUnion.getUnionId());
                    if (unionInfo != null) {
                        itemNum = unionInfo.getContribution();
                    }
                }
                break;
            default:// 其他的普通道具 添加到useritem
                UserItem userItem = getUseritem(userId, itemId);
                if (userItem != null) {
                    itemNum = userItem.getNum();
                }
                break;
        }
        return itemNum;
    }

    // 校验需要的道具数量是否全部足够
    public static boolean checkNeedItemNum(UserInfo userinfo, List<RewardItem> list, Object... entity) {
        if (list.size() == 0) {
            return false;// 避免策划漏配置
        }
        List<RewardItem> needItemList = CommonUtils.mergeItemList(list);
        int count = 0;// 有几个数量达标
        for (int i = 0; i < needItemList.size(); i++) {
            RewardItem rewardItem = needItemList.get(i);
            long useritemNum = getUserItemNum(userinfo, rewardItem.getItemId(), entity);
            if (useritemNum >= rewardItem.getItemNum()) {
                count++;
            }
        }
        if (count > 0 && count >= needItemList.size()) {
            return true;
        } else {
            return false;
        }
    }

    public static boolean checkNeedItemNum(UserInfo userInfo, int itemId, int num) {
        long useritemNum = getUserItemNum(userInfo, itemId);
        return useritemNum >= num;
    }

    // 获取数值类的奖励
    public static void addRewardProp(UserInfo userInfo, RewardItem rewardItem, PBUserData.Builder pbUserData, Object... logParams) {
        int itemId = rewardItem.getItemId();
        long itemNum = rewardItem.getItemNum();
        long userId = userInfo.getId();
        switch (itemId) {
            case ItemId.DIAMOND:// 钻石
                userInfo.addDiamond((int) itemNum, logParams);
                userInfo.update(pbUserData);
                break;
            case ItemId.GOLD:// 金币
                userInfo.addGold(itemNum, logParams);
                userInfo.update(pbUserData);
                break;
            case ItemId.VIP_SCORE:// VIP积分
                userInfo.addVipScore((int) itemNum, logParams);
                userInfo.update(pbUserData);
                GameIapManager.updateVipLevel(pbUserData,userInfo);
                break;
            case ItemId.FREE_VIP_SCORE:// 免费VIP积分
                userInfo.addFreeVipScore((int) itemNum, logParams);
                userInfo.update(pbUserData);
                GameIapManager.updateVipLevel(pbUserData,userInfo);
                break;
            case ItemId.USER_REPU:// 声望
                userInfo.addRepu(pbUserData, userInfo, (int) itemNum, logParams);
                userInfo.update(pbUserData);
                break;
            case ItemId.PARTNER_EXP:// 干员经验
                UserAffair userAffair = GameUser.getUserAffair(userId);
                userAffair.addPartnerExp(userInfo, itemNum, logParams);
                userAffair.update();
                ProtoDataUtils.updatePBUserData(pbUserData, userAffair);
                break;
            case ItemId.DATING_EXP:// 约会经验 羁绊值 干员与秘书约会时获得，可用于提升秘书技能等级
                if (rewardItem.getEntity() != null) {
                    UserSecretary userSecretary = (UserSecretary) rewardItem.getEntity();
                    userSecretary.addFetters(userInfo, (int)itemNum, logParams);
                    userSecretary.update();
                    ProtoDataUtils.updatePBUserData(pbUserData, userSecretary);
                }
                break;
            case ItemId.FIGHT_SNATCH_MONEY:// 100011; // 物资争夺模块商店兑换用的货币
                UserSnatchInfo userSnatchInfo = rewardItem.getEntity() == null ? FightManager.getUserSnatchInfo(userId) : (UserSnatchInfo) rewardItem.getEntity();
                userSnatchInfo.addMoney(userInfo, (int)itemNum, logParams);
                userSnatchInfo.update();
                ProtoDataUtils.updatePBUserData(pbUserData, userSnatchInfo);
                break;
            case ItemId.PERSON:// 100016; // 获得人口 人口不会消耗但不可超过城市人口总容量
                UserCityInfo userCityInfo = BuildManager.getUserCityInfo(userId);
                int max = BuildManager.getCityPeasonMax(userInfo);// 当前等级的人口总量
                if (userCityInfo.getPeasonNum() + itemNum >= max) {
                    itemNum = max - userCityInfo.getPeasonNum();
                    ThinkingDataManager.pushUserPeasonFullEvent(userInfo, userCityInfo.getPeasonNum(), userCityInfo.getHousePeasonNum());// 数数上报数据
                }
                if (itemNum > 0) {
                    userCityInfo.addPeasonNum(pbUserData, userInfo, (int)itemNum, logParams);
                    userCityInfo.update();
                    ProtoDataUtils.updatePBUserData(pbUserData, userCityInfo);
                }
                break;
            case ItemId.FEAST_SCORE:// 100030  获得派对积分
                UserFeastInfo userFeastInfo = FeastManager.getUserFeastInfo(userId);
                userFeastInfo.addScore(pbUserData, userInfo, (int)itemNum, logParams);
                userFeastInfo.update();
                ProtoDataUtils.updatePBUserData(pbUserData, userFeastInfo);
                MainTaskManager.addTaskProgress(pbUserData, userInfo, MainTaskConstant.TASK_TYPE_59, (int)itemNum);// 派对积分增加{0}
                RankActManager.addTaskProgress(pbUserData, userInfo, MainTaskConstant.TASK_TYPE_59, (int)itemNum);
                RankActManager.addUserrank(userInfo.getServerId(), ActTopRanking.RANK_TYPE_FEAST_SCORE, userInfo, itemNum, true);
                RankActManager.addUserCrossRank(userInfo, userInfo.getServerId(), ActTopRanking.RANK_TYPE_FEAST_SCORE, itemNum, true);
                break;
            case ItemId.ENERGY: // 城主体力
                userInfo.addEnergy((int) itemNum, logParams);
                userInfo.update(pbUserData);
                break;
            case ItemId.DATING_ENERGY: // 约会体力
                UserDating userDating = DatingManager.getUserDating(userId);
                userDating.addStrength(userInfo, (int)itemNum, logParams);
                userDating.update();
                ProtoDataUtils.updatePBUserData(pbUserData, userDating);
                break;
            case ItemId.TYPE_ACT_CONVENTION_SCORE: // 万能活动积分
                UserActConvention userActConvention = ActConventionManager.getUserActConvention(userId);
                userActConvention.addScore(userInfo, (int)itemNum, logParams);
                userActConvention.update();
                break;
            case ItemId.TYPE_UNION_CONTRIBUTION: // 联盟贡献
                UserUnion userUnion = UnionManager.getUserUnion(userId);
                if (userUnion.getUnionId() != 0) {
                    userUnion.addDayContribution((int) itemNum);
                    userUnion.addContribution((int) itemNum);
                    userUnion.update();
                    UnionInfo unionInfo = UnionManager.getUnionInfo(userUnion.getUnionId());
                    unionInfo.addContribution((int) itemNum);
                    unionInfo.update();
                    ProtoDataUtils.updatePBUserData(pbUserData, userUnion);
                    ProtoDataUtils.updatePBUserData(pbUserData, unionInfo);
                }
                break;
            case ItemId.TYPE_WORLD_ENERGY: // 100040; 大世界行军模块的体力
                UserWorldInfo userWorldInfo = WorldManager.getUserWorldInfo(userId);
                userWorldInfo.addEnergy(userInfo, (int) itemNum, logParams);
                userWorldInfo.update();
                ProtoDataUtils.updatePBUserData(pbUserData, userWorldInfo);
                break;
            case ItemId.TYPE_WORLD_TEAM_ITEM: // 100042; // 特殊道具，获得的时候解锁大世界行军模块的行军队列
                WorldManager.unlockTeam(pbUserData, userInfo, (int) itemNum);
                break;
            case ItemId.TYPE_WORLD_TEAM_TMP: // 100045; // 使玩家唯一的临时行军队列持续时间增加24小时
                WorldManager.addTmpTeamValidTime(pbUserData, userInfo, (int) itemNum);
                break;
            case ItemId.TYPE_UNION_BOX_ITEM: //  联盟宝箱进度值
                UserUnion userUnion1 = UnionManager.getUserUnion(userId);
                long unionId = userUnion1.getUnionId();
                if (unionId != 0) { // 有联盟才加宝箱进度值
                    UnionInfo unionInfo = UnionManager.getUnionInfo(unionId);
                    if (unionInfo == null) {
                        break;
                    }

                    Lock lock = null;
                    try {
                        lock = MainService.getVertLock(LockKey.getUnionBoxLock(unionId));
                        UnionBoxInfo unionBoxInfo = UnionManager.getUnionBoxInfo(userUnion1.getUnionId());
                        //
                        boolean isAddProgress = true;
                        if (logParams != null && logParams.length > 0 ){
                            Object lastParam = logParams[logParams.length - 1];
                            int type = lastParam instanceof Number ? ((Number)lastParam).intValue() : 0; // 默认值
                            String typ =  lastParam instanceof String ? ((String)lastParam) : "0"; // 默认值
                           if ((type == UnionConstant.UNION_GIFT_BOX_TYPE_1 || typ.equals("1") ) && unionBoxInfo != null){
                               if(unionBoxInfo.getAddValue() >= AllParam.UNION_BOX_MAX){
                                   itemNum = 0;
                                   isAddProgress = false;
                               } else if (unionBoxInfo.getAddValue() < AllParam.UNION_BOX_MAX) {
                                   itemNum = Math.min(itemNum, Math.max(0,AllParam.UNION_BOX_MAX - unionBoxInfo.getAddValue()));
                                   unionBoxInfo.addAddValue(itemNum);
                               }

                           }
                        }
                        if (isAddProgress){
                            boolean addProgress = unionBoxInfo.addProgress((int) itemNum, unionInfo.getLevel());
                            if (addProgress){  // 如果有宝箱变化
                                UnionManager.sendUnionGiftRedPoint(unionInfo);  // 发送联盟大礼红点
                            }
                        }
                        unionBoxInfo.update();
                        ProtoDataUtils.updatePBUserData(pbUserData, unionBoxInfo);
                    } catch (LockException e) {  // 先捕获具体异常
                        throw new RuntimeException("Failed to acquire union box lock for unionId: " + unionId, e);
                    } catch (Exception e) {
                        throw new RuntimeException("处理联盟宝箱时发生错误", e);
                    } finally {
                        MainService.releaseVertLock(lock);
                    }

                }
                break;
            default:// 其他的普通道具 添加到useritem
                Item item = getItem(itemId);
                Box box = getBox(itemId);
                if (item == null && box == null) {// 既不是道具也不是宝箱
                    return;
                }
                UserItem userItem = getUseritem(userId, itemId);
                if (userItem == null) {
                    userItem = new UserItem(userId, itemId, itemNum);
                    userItem.doCreate();
                    MotuLogManager.logItemIncome(userInfo, itemId, userItem.getId(), 0, itemNum, logParams);// 记录获得日志
                } else {
                    userItem.addNum(userInfo, itemNum, logParams);
                }
				// 像UserItem这种叠加道具，如果通知客户端删除之后再连续获得，那么第二次update对象会覆盖第一次的完整对象，导致客户端收不到完整对象，所以直接下发完整对象
                ProtoDataUtils.updatePBUserData(pbUserData, userItem, true);
                break;
        }
    }

    public static UserSecretary getUserSecretary(long userId, Object... param) {
        if (param[0] instanceof Integer) {
            int secretaryId = (int) param[0];
            return SecretaryManager.getUserSecretary(userId, secretaryId);
        } else if (param[0] instanceof UserSecretary) {
            return (UserSecretary) param[0];
        }
        return null;
    }


    // 扣除数值类的奖励
    public static void subRewardProp(UserInfo userInfo, RewardItem rewardItem, PBUserData.Builder pbUserData, Object... logParams) {
        int itemId = rewardItem.getItemId();
        long itemNum = rewardItem.getItemNum();
        long userId = userInfo.getId();
        switch (itemId) {
            case ItemId.DIAMOND:// 钻石
                userInfo.subDiamond((int) itemNum, logParams);
                userInfo.update(pbUserData);
                RankActManager.addUserrank(userInfo.getServerId(), ActTopRanking.RANK_TYPE_DIAMOND_COST, userInfo, itemNum, true);
                RankActManager.addUserCrossRank(userInfo, userInfo.getServerId(), ActTopRanking.RANK_TYPE_DIAMOND_COST, itemNum, true);
                MainTaskManager.addTaskProgress(pbUserData, userInfo, MainTaskConstant.TASK_TYPE_90, itemNum);// 更新主线任务,成就,日常任务的进度
                break;
            case ItemId.GOLD:// 金币
                userInfo.subGold(itemNum, logParams);
                userInfo.update(pbUserData);
                break;
            case ItemId.PARTNER_EXP:// 经验
                UserAffair userAffair = GameUser.getUserAffair(userId);
                userAffair.subPartnerExp(userInfo, itemNum, logParams);
                userAffair.update();
                ProtoDataUtils.updatePBUserData(pbUserData, userAffair);
                break;
            case ItemId.DATING_EXP:
                if (rewardItem.getEntity() != null) {
                    UserSecretary userSecretary = (UserSecretary) rewardItem.getEntity();
                    if (userSecretary != null) {
                        userSecretary.subFetters(userInfo, (int) itemNum, logParams);
                        userSecretary.update();
                        ProtoDataUtils.updatePBUserData(pbUserData, userSecretary);
                    }
                }
                break;
            case ItemId.ENERGY: // 体力
                userInfo.subEnergy((int) itemNum, logParams);
                userInfo.update(pbUserData);
                break;
            case ItemId.FIGHT_SNATCH_MONEY:// 100011; // 物资争夺模块商店兑换用的货币
                UserSnatchInfo userSnatchInfo = FightManager.getUserSnatchInfo(userId);
                userSnatchInfo.subMoney(userInfo, (int)itemNum, logParams);
                userSnatchInfo.update();
                ProtoDataUtils.updatePBUserData(pbUserData, userSnatchInfo);
                break;
            default:// 其他的普通道具 添加到useritem
                UserItem userItem = BagManager.getUseritem(userId, itemId);
                userItem.subdNum(userInfo, itemNum, logParams);
                if (userItem.getNum() <= 0) {// 通知客户端删除
                    ProtoDataUtils.deletePBUserData(pbUserData, userItem);
                } else {
                    ProtoDataUtils.updatePBUserData(pbUserData, userItem);// 把变动的数据下发给客户端
                }
                break;
        }
        MainTaskManager.addTaskProgress(pbUserData, userInfo, MainTaskConstant.TASK_TYPE_60, (int)itemNum, itemId);// 更新主线任务,成就,日常任务的进度
        RankActManager.addTaskProgress(pbUserData, userInfo, MainTaskConstant.TASK_TYPE_60, (int)itemNum, itemId);
    }

    // 打开宝箱获得奖励,只适用固定宝箱类,和随机宝箱类  type=0, 1这两个类型
    public static List<RewardItem> takeRewardFromBox(Box box, int num) {
        List<RewardItem> rewardItemList = new ArrayList<>();
        if (box.getType() == BagConstant.BOX_REWARD_TYPE_1) {// 随机宝箱类,使用后随机获得宝箱里的一项道具
            for (int i=0; i<num; i++) {
                RewardItem rewardItem = getBoxRandomReward(box);
                rewardItemList.add(rewardItem);
            }
        } else if (box.getType() == BagConstant.BOX_REWARD_TYPE_0) {// 固定宝箱类,使用后获得宝箱里所有的道具
            List<BoxItem> list = getBoxItemList(box.getBoxId());// 这个宝箱的所有奖励
            for (int i=0; i< list.size(); i++)  {
                BoxItem boxItem = list.get(i);
                RewardItem rewardItem = new RewardItem(boxItem.getItemId(), boxItem.getItemNum() * num);
                rewardItemList.add(rewardItem);
            }
        }
        return CommonUtils.mergeItemList(rewardItemList);// 合并相同itemid的数量
    }

    // 获得宝箱的随机奖励
    public static RewardItem getBoxRandomReward(Box box) {
        List<BoxItem> list = getBoxItemList(box.getBoxId());
        int[] rates = new int[list.size()];
        for (int i=0; i< list.size(); i++)  {
            rates[i] = list.get(i).getRate();
        }
        int randon = Tool.getWeightIndex(rates);
        BoxItem boxItem = list.get(randon);// 随机到的奖励
        return new RewardItem(boxItem.getItemId(), boxItem.getItemNum());
    }

    // 打开随机宝箱奖励 + 必得奖励
    public static List<RewardItem> takeBoxRandomRewardAndMustReward(Box box, int num) {
        List<RewardItem> rewardItemList = new ArrayList<>();
        for (int i=0; i<num; i++) {
            RewardItem rewardItem = getBoxRandomReward(box);
            rewardItemList.add(rewardItem);
        }

        List<RewardItem> rewardItems = CommonUtils.takeReawrdItemFromStr(box.getReward()); // 必得奖励（单个）
        CommonUtils.multipleItemList(rewardItems, num);//数量 x N倍
        rewardItemList.addAll(rewardItems);
        return CommonUtils.mergeItemList(rewardItemList);// 合并相同itemid的数量
    }

    // 背包的红点,仓库红点
    public static boolean checkRedPointBag(CSGameSystemRedPointResponse.Builder builder, UserInfo userInfo){
        // 是否有可以合成的道具
        CsGameSystem.RedPoint_Bag.Builder redPoint = CsGameSystem.RedPoint_Bag.newBuilder();
        for (Item item : getItemMap().values()) {
            if (item.isComposeItem() && checkNeedItemNum(userInfo, CommonUtils.takeReawrdItemFromStr(item.getComposeItem()))) {// 是合成道具
                redPoint.setIsCompose(true);
                builder.setBag(redPoint);
                return true;
            }
        }

        return false;
    }

    /**
     * 添加碎片奖励
     * @param userInfo
     * @param chipId
     * @param chipNum
     * @param pbUserData
     * @param logParams
     */
    public static void addChipReward(UserInfo userInfo,int chipId, long chipNum, PBUserData.Builder pbUserData, Object... logParams) {
        long userId = userInfo.getId();
        UserChip userChip = BagManager.getUserChip(userId, chipId);
        if (userChip == null) {
            userChip = new UserChip(userId, chipId, chipNum);
            userChip.doCreate();
            MotuLogManager.logItemIncome(userInfo, chipId, userId, 0, chipNum, logParams);// 记录获得日志
        } else {
            userChip.addChipNum(userInfo, chipNum, logParams);
        }
        ProtoDataUtils.updatePBUserData(pbUserData, userChip, true);
    }

    /**
     * 扣除碎片
     * @param userInfo
     * @param chipId
     * @param chipNum
     * @param pbUserData
     * @param logParams
     */
    public static void subChipReward(UserInfo userInfo,int chipId, long chipNum, PBUserData.Builder pbUserData, Object... logParams) {
        long userId = userInfo.getId();
        UserChip userChip = getUserChip(userId, chipId);
        userChip.subChipNum(userInfo, chipNum, logParams);
        if (userChip.getNum() <= 0) {
            ProtoDataUtils.deletePBUserData(pbUserData, userChip);
        } else {
            ProtoDataUtils.updatePBUserData(pbUserData, userChip);
        }
    }

    /**
     * 检查碎片数量是否足够
     * @param userId
     * @param chipId
     * @param num
     * @return
     */
    public static boolean checkNeedChipNum(long userId, int chipId, long num) {
        UserChip userChip = getUserChip(userId, chipId);
        if (userChip == null) {
            return false;
        }
        return userChip.getNum() >= num;
    }

    // 根据碎片id 获得干员，秘书，武器 等id
    public static int takePartnerSecrIdByChip(int chipId) {
        return chipId - GameConstant.START_ID_CHIP_MAIN;
    }

    /**
     * 开宝箱
     * @param pbUserData
     * @param userInfo
     * @param box  宝箱对象 只支持type=0,1,2,3  这几种
     * @param boxNum 数量
     * @param selectId 自选宝箱的box_item 的id 字段，非自选宝箱传0
     */
    public static void openBox(PBUserData.Builder pbUserData, UserInfo userInfo, Box box, int boxNum, int selectId, Object... logParams) {
        List<RewardItem> rewardItemList = getBoxReward(box, boxNum, selectId);
        if (rewardItemList.size() > 0) {
            RewardManager.addReward(userInfo, rewardItemList, pbUserData, logParams);// 获得相应奖励
        }
    }


    // 返回宝箱里的奖励内容
    public static List<RewardItem> getBoxReward(Box box, int boxNum, int selectId) {
        List<RewardItem> rewardItemList = new ArrayList<>();
        if (box.getType() == BagConstant.BOX_REWARD_TYPE_0 || box.getType() == BagConstant.BOX_REWARD_TYPE_1) {
            rewardItemList = BagManager.takeRewardFromBox(box, boxNum);
        } else if (box.getType() == BagConstant.BOX_REWARD_TYPE_2) {// 自选宝箱类,使用后获得宝箱里所有的道具
            BoxItem boxItem = BagManager.getBoxItem(selectId);
            if (boxItem != null && boxItem.getBoxId() == box.getBoxId()) {
                RewardItem rewardItem = new RewardItem(boxItem.getItemId(), boxItem.getItemNum() * boxNum);
                rewardItemList.add(rewardItem);
            }
        } else if (box.getType() == BagConstant.BOX_REWARD_TYPE_3) {
            rewardItemList = BagManager.takeBoxRandomRewardAndMustReward(box, boxNum);
        }
        return rewardItemList;
    }

    /**
     * 获得宝箱的时候，自动打开，获得里面的配置奖励
     * @param pbUserData
     * @param userInfo
     * @param box
     * @param boxNum
     * @param logParams
     */
    public static void autoOpenBoxReward(PBUserData.Builder pbUserData, UserInfo userInfo, Box box, int boxNum, Object... logParams) {
        if (box.getIsOpen() == 0) {
            return;
        }
        if (box.getType() != BagConstant.BOX_REWARD_TYPE_0 && box.getType() != BagConstant.BOX_REWARD_TYPE_1) {
            return;
        }
        openBox(pbUserData, userInfo, box, boxNum, 0, logParams);
    }
}
